package leetcode.problems;

/**
 * Created by Administrator on 2018/3/15.
 */
public class _0315test {
    /**
     * S and T are strings composed of lowercase letters. In S, no letter occurs more than once.
     * S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.
     * Return any permutation of T (as a string) that satisfies this property.
     * Example :
     * Input:
     * S = "cba"
     * T = "abcd"
     * Output: "cbad"
     * Explanation:
     * "a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
     * Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.
     * S和T是由小写字母组成的字符串。在S中，所有字母均出现一次（不会重复出现）。
     * S是以特定的顺序排列。我们要改变T的字母顺序以使T符合S的排列顺序。更具体地说，如果在S中x出现y之前，那么在返回字符串中x应该出现的y之前。
     * 返回满足这个满足顺序的所有字符序列。
     * 例如:
     * 输入：
     * S = "cba"
     * T = "abcd"
     * 输出："cbad"
     * 解释：
     * “A”、“B”、“C”出现在s中，所以“A”、“B”、“C”的顺序应为“C”、“B”和“A”。
     * 由于“D”不会出现的，它可以在任何位置T."dcba", "cdba", "cbda"也是有效的输出。
     */
    public static String test(String s, String t) {
        StringBuilder sb = new StringBuilder(s);
        for (int i = 0; i < t.length(); i++) {
            char s1 = t.charAt(i);
            if(!s.contains(s1+"")){
                sb.append(s1);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = "cba";
        String t = "abcd";
        String out = test(s, t);
        System.out.println(out);
    }
}
